﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;
namespace WFWS.ClientConsole
{

    public static class ThreadId
    {
        public static int MaxCount;

        public static object LockObj = new object();
    }

    class Program
    {
        static void Main(string[] args)
        {
            ///模拟用户并发请求：

            //Thread.Sleep(5000);
            //Client c = new Client(100);
            //c.Go();
            //Console.ReadLine();

            int count = 0;
            var step = 4000;
            
            //测试长连接：
            for (int i = 0; i < step; i++)
            {
                LongConnect();
            }

            count += step;
            Console.WriteLine(count);
            while (true)
            {
                while (Console.Read() == 'd')
                {
                    for (int i = 0; i < step; i++)
                    {
                        LongConnect();
                    }
                    count += step;

                    Console.WriteLine(count);
                }
            }
       

            Console.ReadLine();
            Console.ReadLine();
            Console.ReadLine();
            Console.ReadLine();
            return;
        }

        static void LongConnect()
        {
            WebRequest req = WebRequest.Create("Http://localhost:808");
            req.BeginGetResponse(CallBack, req);
        }

        static byte[] _buffer = new byte[4096];
        static void CallBack(IAsyncResult ar)
        {
            var req = (WebRequest)ar.AsyncState;
            var response = req.EndGetResponse(ar);
            var stream = response.GetResponseStream();
            stream.BeginRead(_buffer, 0, _buffer.Length, ReadCallback, stream);
        }

        static void ReadCallback(IAsyncResult ar)
        {
            var stream = (Stream)ar.AsyncState;
            var size = stream.EndRead(ar);
            
            if(size>0)
                stream.BeginRead(_buffer, 0, _buffer.Length, ReadCallback, stream);

            //int i = 0;
            //while (size > 0 && i < size)
            //{
            //    Console.Write((char)_buffer[i]);
            //    i++;
            //    size--;
            //}
        }
    }



    public class Client
    {
        private void Call(IAsyncResult ar)
        {
            WebRequest request = ar.AsyncState as WebRequest;
            var response = request.EndGetResponse(ar);
            var stream = response.GetResponseStream();
            using (var reader = new StreamReader(stream))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
        }

        public void Go()
        {
            ThreadPool.SetMinThreads(8, 8);
            ThreadPool.SetMaxThreads(100, 100);

            for (int i = 0; i < Number; i++)
            {
                WebRequest request = WebRequest.Create("Http://localhost:808");
                request.BeginGetResponse(new AsyncCallback(Call), request);
            }
        }

        public void TcpRequest()
        {
            
        }

        public int Number { get; set; }
        public Client(int number)
        {
            Number = number;
        }

        public static void Get(object number)
        {
            Console.WriteLine("worker {0} start! and thread id is {1}. ", number , Thread.CurrentThread.ManagedThreadId);
    
            //Thread.Sleep(1000);
            WebRequest request = WebRequest.Create("http://www.liulijin.info/");
            Console.WriteLine("Request Created");
            var response = request.GetResponse();
            Console.WriteLine("Response received");
            StreamReader reader = new StreamReader(response.GetResponseStream());
            Console.WriteLine(String.Format("{0}----{1}",number,reader.ReadToEnd()));
            Console.WriteLine("worker {0} finish!", number);
        }
    }
}
